-- 运营活动: 达标竞技


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 44, -- 活动id (44 充值排行)
    act_name = "充值排行", -- 活动名称
    act_desc = "活动结束时根据充值排名领取丰厚奖励！", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1593267400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        needMinValue = 300, --上榜最低要求
        exAward = 
        {
            desc = "充值1000元宝奖励",
            needValue = 1000,
            icon = 1,
            awards = 
            {
                { type = 0, id = 851, count = 1, bind = 1, },
                { type = 0, id = 565, count = 1, bind = 1, },
                { type = 0, id = 559, count = 6, bind = 1, },
                { type = 0, id = 763, count = 100, bind = 1, },
            },
        },
        Awards =
        {
            {
                desc = "充值第1名",
                icon = 1,
                cond = {1, 1},
                awards = 
                {
                    { type = 0, id = 851, count = 1, bind = 1, },
                    { type = 0, id = 565, count = 1, bind = 1, },
                    { type = 0, id = 559, count = 6, bind = 1, },
                    { type = 0, id = 763, count = 100, bind = 1, },
                },
            },
            {
                desc = "充值第2-4名",
                icon = 1,
                cond = {2, 4},
                awards = 
                {
                    { type = 0, id = 518, count = 6, bind = 1, },
                    { type = 0, id = 564, count = 6, bind = 1, },
                    { type = 0, id = 559, count = 1, bind = 1, },
                    { type = 0, id = 763, count = 60, bind = 1, },
                },
            },
            {
                desc = "充值第5-10名",
                icon = 1,
                cond = {5, 10},
                awards = 
                {
                    { type = 0, id = 518, count = 4, bind = 1, },
                    { type = 0, id = 564, count = 4, bind = 1, },
                    { type = 0, id = 763, count = 40, bind = 1, },
                },
            },
            {
                desc = "充值第11-20名",
                icon = 1,
                cond = {11, 20},
                awards = 
                {
                    { type = 0, id = 518, count = 2, bind = 1, },
                    { type = 0, id = 564, count = 2, bind = 1, },
                    { type = 0, id = 763, count = 20, bind = 1, },
                },
            },
            {
                desc = "充值第21-50名",
                icon = 1,
                cond = {21, 50},
                awards = 
                {
                    { type = 0, id = 518, count = 1, bind = 1, },
                    { type = 0, id = 564, count = 1, bind = 1, },
                },
            },
        },
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_80_check_cfg(cfg)
	if cfg == nil then return 20 end
	
	local config = cfg.config
	if config == nil then return 21 end
	
	if config.Awards == nil then return 23 end
	if type(config.Awards) ~= "table" then return 24 end
	for idx = 1, #config.Awards do
		local dayRewards = config.Awards[idx]
		if dayRewards == nil then return 25 end
		if type(dayRewards) ~= "table" then return 26 end
		if dayRewards.awards == nil then return 27 end
		if type(dayRewards.awards) ~= "table" then return 28 end
		if not oper_act_check_one_award_cfg(dayRewards.awards) then
			return 22
		end	
	end
    if config.exAward == nil then return 29 end
    if type(config.exAward) ~= "table" then return 30 end
    if config.exAward.awards == nil then return 31 end
    if type(config.exAward.awards) ~= "table" then return 32 end
    if not oper_act_check_one_award_cfg(config.exAward.awards) then
        return 22
    end 

    if config.exAward.needValue < config.needMinValue then
        return 33
    end

	return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_80_setup(cfg)	
	return 0
end

-- 卸载活动
function oper_act_80_unsetup(cfg)	
end

-- 活动开始
function oper_act_80_start(cfg)
	local ssVar = System.getStaticVar()
    ssVar.oper_act_80_start = 1
	ssVar.oper_act_80_cmd = cfg.cmd_id
	ssVar.oper_act_80_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_80, 1)

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            return 
        end
    end
    
    gRankMgr_Clear(RANK_DEFINE_OPERATION_80)
end

-- 活动结束
function oper_act_80_end(cfg)
	local ssVar = System.getStaticVar()
	ssVar.oper_act_80_start = nil
	ssVar.oper_act_80_cmd = nil
	ssVar.oper_act_80_act = nil

	if not cfg then return end
	local config = cfg.config
	if not config then return end

    local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_80].strName)
    if rankPtr then
        local rankCount = Ranking.getRankItemCount(rankPtr)
        for idx = 1, rankCount do
            local item = Ranking.getItemFromIndex(rankPtr, idx - 1)
            if item then
                local myScore = Ranking.getPoint(item)    
                if myScore < config.needMinValue then
                    return
                else
                    local actorId = tonumber(Ranking.getId(item))
                    local data = gRankMgr_GetValueById(actorId, RANK_DEFINE_OPERATION_80, 1)
                    local myData = BaseFuc_unserialize(data)
                    local awardConfig = GetElemByCond(config.Awards, idx)
                    if awardConfig then
                        local title = string.format(Lang.ScriptTips.Operation117, idx)
                        local content = string.format(Lang.ScriptTips.Operation118, idx)

                        Awards.GiveToMailById(actorId, awardConfig.awards, title, content, LogIds.OperationLogId080, Lang.LogTips.Operation080, myData.vocation, myData.sex)    

                    end
                    if myData and myData.myAwardState and myData.myAwardState == 1 then
                        local extitle = Lang.ScriptTips.Operation216
                        local excontent = Lang.ScriptTips.Operation217
                        Awards.GiveToMailById(actorId, config.exAward.awards, extitle, excontent, LogIds.OperationLogId080, Lang.LogTips.Operation080, myData.vocation, myData.sex) 
                    end
                end
            end
        end
    end
end

-- 打包数据
function oper_act_80_packet_data(sysarg, cfg)
	if not cfg then return end
	local config = cfg.config
	if not config then return end
	
    local myIdx = 0
    local myScore = 0
    local myAwardState = 0
    local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_80].strName)
    if rankPtr then
        local actorid = Actor.getActorId(sysarg)
        local item = Ranking.getItemPtrFromId(rankPtr, actorid)
        if item then
            myIdx = Ranking.getIndexFromPtr( item ) + 1
            --print("myIdx ="..myIdx)
            myScore = Ranking.getPoint(item)  
            --print("myScore ="..myScore)
            --print("config.needMinValue ="..config.needMinValue)  
            if myScore < config.needMinValue then
                myIdx = 0
                --print("myIdx ="..myIdx)
            end

            local data = Ranking.getSub(item, 1)
            local myData
            if not data or data == "" then
                myData = {}
            else
                myData = BaseFuc_unserialize(data)
            end
            if not myData then
                myData = {}
            end

            if not myData.myAwardState then
                myData.myAwardState = 0
            end
            myAwardState = myData.myAwardState
        end
    end
    
	local npack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (npack == nil) then
        return
    end

    DataPack.writeShort(npack, myIdx)
    DataPack.writeInt(npack, myScore)
    DataPack.writeByte(npack, myAwardState)
	DataPack.flush(npack) 
end

-- 操作，返回操作结果，0为成功
function oper_act_80_do(sysarg, cfg, clientPkt)
	if cfg == nil then return end
	
	if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
		return
	end
	
	local config = cfg.config
	if config == nil then -- 参数错误
		print("error, oper_act_80_do cfg.config confg error")
		return
	end
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_80, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end

    if not myData.myAwardState then
        myData.myAwardState = 0
    end

    if myData.myAwardState == 0 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.Operation006, ttFlyTip)
        return
    end
    if myData.myAwardState == 2 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.Operation220, ttFlyTip)
        return
    end

    myData.myAwardState = 2
    if IsBagGridEnough(sysarg, config.exAward.awards) then
        GiveCommonAward(sysarg, config.exAward.awards, LogIds.OperationLogId080, Lang.LogTips.Operation080)
    else
        local actorId = Actor.getActorId(sysarg)
        local title = Lang.ScriptTips.Operation216
        local content = Lang.ScriptTips.Operation217

        local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)

        Awards.GiveToMailById(actorId, config.exAward.awards, title, content, LogIds.OperationLogId080, Lang.LogTips.Operation080, vocation, sex)
    end

    
    local time = System.getSystemTime()
    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_80, 1, newData)

     oper_act_80_packet_data(sysarg, cfg)
end

--[[
-- 跨天
function oper_act_80_new_day_arrive(act_id)	

end
]]


function oper_act_80_withdrawyuanbao(sysarg, value)
    if value <= 0 then
        return
    end

	local ssVar = System.getStaticVar()
	if not ssVar.oper_act_80_start then
		return
	end
	if not ssVar.oper_act_80_cmd then
		return
	end
	if not ssVar.oper_act_80_act then
		return
	end
	
	local cfg = get_oper_act_cfg(ssVar.oper_act_80_cmd, ssVar.oper_act_80_act)
	if not cfg then
		return
	end

	local config = cfg.config
	if not config then
		return
	end	

    local actorid = Actor.getActorId(sysarg)
    --gRankMgr_SetRank(actorId, nRankId, newValue)
    gRankMgr_AddValue(actorid, RANK_DEFINE_OPERATION_80, value)

    local value = gRankMgr_GetValue(actorid, RANK_DEFINE_OPERATION_80)

    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_80, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end

    if not myData.myAwardState then
        myData.myAwardState = 0
    end

    if value >= config.exAward.needValue and myData.myAwardState == 0 then
        myData.myAwardState = 1
    end
    
    local time = System.getSystemTime()
    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end
    
    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_80, 1, newData)
    
	oper_act_80_packet_data(sysarg, cfg)
	
end

